package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.logic.PersonLogic;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.resources.OrganizeJoinRes;
import com.jp.knowledge.my.util.MyUtil;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

/**
 * 创建组织
 *
 * @author gzejia 978862664@qq.com
 */
public class CompanyCreateActivity extends SlidingActivity implements ResultSubscriber.OnResultListener {

    private static final int SELECT_COMPANY_OCCUPATION = 0x01;
    private static final int SELECT_COMPANNY_SIZE = 0x02;

    @ViewInject(R.id.create_company_edt_name)
    private EditText mCompanyEdt;
    @ViewInject(R.id.create_company_tv_trade)
    private TextView mTradeTv;
    @ViewInject(R.id.create_company_tv_size)
    private TextView mSizeTv;

    private int type;
    private int level;
    private String industryId;

    @Override
    protected int getResourceId() {
        return R.layout.activity_company_create;
    }

    @Event(value = {R.id.icon_left, R.id.create_company_lly_trade, R.id.create_company_lly_size, R.id.join_company_btn_request},
            type = View.OnClickListener.class)
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.icon_left:// 返回
                finish();
                break;
            case R.id.create_company_lly_trade:// 行业
                Intent occupationIntent = new Intent(this, OptionCompanyOccupationActivity.class);
                occupationIntent.putExtra(OrganizeJoinRes.TITLE, "选择行业类型");
                startActivityForResult(occupationIntent, SELECT_COMPANY_OCCUPATION);
                break;
            case R.id.create_company_lly_size:// 规模
                Intent sizeIntent = new Intent(this, OptionCompanySizeActivity.class);
                sizeIntent.putExtra(OrganizeJoinRes.TITLE, "选择公司规模");
                startActivityForResult(sizeIntent, SELECT_COMPANNY_SIZE);
                break;
            case R.id.join_company_btn_request:// 创建
                if(level <= 0
                        || industryId == null
                        || industryId.trim().length() == 0
                        || mCompanyEdt.getText().toString().trim().length() == 0){
                    ToasUtil.toast(this, "请确保信息完整后再提交");
                    return;
                }
                loading.show();
                JsonObject param = new JsonObject();
                param.addProperty("token", HttpHelper.token);
                param.addProperty("name", mCompanyEdt.getText().toString());
                param.addProperty("type", type);
                param.addProperty("industryId", industryId);
                param.addProperty("level", level);
                HttpHelper.getInstance(this).companyCreate(param, 0x01, this);
                break;
        }
    }

    @Override
    protected void init() {
        type = getIntent().getIntExtra("type", 1);
        level = 0;
        industryId = null;
        initTitle();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyUtil.closeKeyword(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || null == data)
            return;
        switch (requestCode) {
            case SELECT_COMPANY_OCCUPATION:
                industryId = data.getStringExtra(OptionCompanyOccupationActivity.INDUSTRY_ID);
                mTradeTv.setText(data.getStringExtra(OrganizeJoinRes.RESPONSE_RESULT));
                break;
            case SELECT_COMPANNY_SIZE:
                level = data.getIntExtra(OptionCompanySizeActivity.COMPANY_LEVEL, 0);
                mSizeTv.setText(data.getStringExtra(OrganizeJoinRes.RESPONSE_RESULT));
                break;
        }
    }

    private void initTitle() {
        topName.setText(R.string.create_company_tv_main);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        topDesc.setVisibility(View.GONE);
        rightIcon.setVisibility(View.GONE);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        loading.cancel();
        ToasUtil.toast(this, "抱歉，创建失败，请稍后重试");
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(requestType != 0x01)
            return;
        ToasUtil.toast(this, "创建成功");
//        startActivity(new Intent(this, OrganizeActivity.class));

//        Intent intent = new Intent(this, SearchListActivity.class);
//        intent.putExtra(OrganizeJoinRes.RESPONSE_RESULT, mCompanyEdt.getText().toString());
//        intent.putExtra(OrganizeJoinRes.ID, data.getEntity(ResultDataModel.class).getId());
//        setResult(RESULT_OK, intent);
//        finish();

        startActivity(new Intent(this, OrganizeActivity.class));
        new PersonLogic(mContext).getUserData();//创建成功要重新获取用户信息
        finish();
    }

    private class ResultDataModel{

        /**
         * id : 40b0b9ad-0b7f-432b-900b-315aef8c6a39
         * type : 1
         */

        private String id;
        private int type;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }
}
